<style scoped>
    .layui-form >>> .layui-form-select{
        z-index: inherit;
    }
    .layui-form-label {
        width: 100px;
    }
    .layui-input-block {
        margin-left: 130px;
    }
</style>

<template>
    <div class="company">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>添加订单</legend>
        </fieldset>

        <form class="layui-form " >
            <div class="layui-form-item">
                <label class="layui-form-label">订单名称</label>
                <div class="layui-input-block">
                    <input type="text" name="entity.name" lay-verify="required" placeholder="请输入订单名称" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">所属部门</label>
                <div class="layui-input-block">
                    <select type="text" name="entity.departmentguid" lay-filter="depart" lay-verify="required" class="layui-select" >
                        <option value=""></option>
                        <option v-for="(typ, index) in departments" :value="typ.guid">{{typ.name}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">种植户</label>
                <div class="layui-input-block">
                    <select type="text" name="entity.farmerid" lay-verify="required" class="layui-select"  lay-search>
                        <option value="">请选择</option>
                        <option v-for="(typ, index) in farmers" :value="typ.userid">{{typ.name}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">种植户类型</label>
                <div class="layui-input-block">
                    <select type="text" name="entity.type" lay-verify="required" class="layui-select" >
                        <option value="">请选择</option>
                        <option v-for="(typ, index) in types" :value="typ.dictkey">{{typ.dicttext}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">订单年份</label>
                <div class="layui-input-block">
                    <select type="text" name="entity.yearoforder" lay-verify="required" class="layui-select" >
                        <option value="">请选择</option>
                        <option v-for="(typ, index) in years" :value="typ" :selected="typ == currYear">{{typ}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">批次</label>
                <div class="layui-input-block">
                    <select type="text" name="entity.orderbatchguid" lay-filter="orderbatch" lay-verify="required" class="layui-select" >
                        <option value="">请选择</option>
                        <option v-for="(typ, index) in batchs" :value="typ.guid">{{typ.name}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">种植面积(亩)</label>
                <div class="layui-input-block">
                    <input type="number" name="entity.orderarea" lay-verify="required" placeholder="请输入种植面积" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">种植品种</label>
                <div class="layui-input-block">
                    <input type="text" name="entity.productname" lay-verify="required" placeholder="请输入品种" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">种薯销售数量(吨)</label>
                <div class="layui-input-block">
                    <input type="number" name="entity.seedweight" placeholder="请输入种薯销售数量" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">农药销售数量(吨)</label>
                <div class="layui-input-block">
                    <input type="number" name="entity.pestweight" placeholder="请输入农药销售数量" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">化肥销售数量(吨)</label>
                <div class="layui-input-block">
                    <input type="number" name="entity.fertweight" placeholder="请输入化肥销售数量" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">订单签订负责人</label>
                <div class="layui-input-block">
                    <input type="hidden" name="entity.orderownername" ref="orderownername" />
                    <select type="text" name="entity.orderownerid" lay-filter="orderowner" lay-verify="required" class="layui-select"  lay-search>
                        <option value="">请选择</option>
                        <option v-for="(typ, index) in owners" :value="typ.guid">{{typ.realname}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">种植过程负责人</label>
                <div class="layui-input-block">
                    <input type="hidden" name="entity.plantownername" ref="plantownername" />
                    <select type="text" name="entity.plantownerid" lay-filter="plantowner" lay-verify="required" class="layui-select"  lay-search>
                        <option value="">请选择</option>
                        <option v-for="(typ, index) in owners" :value="typ.guid">{{typ.realname}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="submit">立即提交</button>
                    <button class="layui-btn layui-btn-primary" @click="$parent.back">返回</button>
                </div>
            </div>
        </form>
    </div>
</template>

<script>
    export default {
        name: 'AddFarmerOrder',
        props: ['id'],
        data () {
            return {
                departments: [],
                batchs: [],
                farmers: [],
                types: [],
                owners: [],
                years: [],
                currYear: '',
            }
        },
        mounted() {
            let that = this;
            layui.use(['form'], function () {
                var form = layui.form,
                    layer = layui.layer;

                //获取部门
                that.getDepartment();
                //获取订单签订负责人、种植过程负责人
                that.getUsers(2);
                that.getTypes();
                that.generateYear();
                form.on('select(depart)', function (data) {
                    //获取种植户
                    that.getFramers(data.value);
                    //获取批次
                    that.getBatchs(data.value);
                })
                form.on('select(orderowner)', function (data) {
                    that.$refs.orderownername.value = that.checkObjNull(that.owners.filter(item => {
                        return data.value == item.guid;
                    })[0]).realname;
                })
                form.on('select(plantowner)', function (data) {
                    that.$refs.plantownername.value = that.checkObjNull(that.owners.filter(item => {
                        return data.value == item.guid;
                    })[0]).realname;
                })
                //提交编辑过的数据
                form.on('submit(submit)', function (data) {
                    let loader = layer.load();
                    $.ajax({
                        url: '/farmer/Farmerorder!save.action',
                        data: data.field,
                        type: "post",
                        dataType: "json",
                        success: function(res) {
                            if (res.msg == "success") {
                                that.toast("添加成功");
                                that.$parent.switchPage('FarmerOrder');
                            } else{
                                that.toast("系统错误");
                            }
                        },
                        complete: function () {
                            layer.close(loader);
                        }
                    });
                    return false;
                });
            });
        },
        updated() {
            layui.form.render();
        },
        methods: {
            getDepartment: function () {
                let that = this;
                this.axios({
                    method: 'get',
                    responseType: 'json',
                    url: '/sales/Sellform!findDepartment.action',
                    params: {sPermission: 41}
                }).then(response => {
                    if (response.data.status == "true") {
                        that.departments = response.data.result;
                    } else if (response.data.status == "false") {
                        this.toastText(response.data.result);
                    }  else {
                        this.toastText('获取部门数据失败');
                    }
                });
            },
            generateYear: function() {
                let date = new Date();
                let years = [];
                for (let i=1; i<11; i++) {
                    years.push(date.getFullYear() - 5 + i);
                }
                this.currYear = date.getFullYear();
                this.years = years;
            },
            getUsers: function (type) {
                let that = this;
                let postParam = {};
                postParam['entity.userType'] = type;
                this.axios({
                    method: 'get',
                    url: '/system/Tuser!getUserAll.action',
                    params: postParam
                }).then(response => {
                    if (response.data.info != undefined && response.data.info.length > 0) {
                            that.owners = response.data.info;
                    }  else {
                            this.toastText('获取负责人数据失败');
                    }
                });
            },
            getFramers: function (departid) {
                let that = this;
                this.axios({
                    method: 'get',
                    url: '/farmer/Farmerorder!getFarmers.action',
                    params: {Deptguid: departid}
                }).then(response => {
                    if (response.data.msg == "success") {
                        that.farmers = response.data.farmers;
                    }  else {
                        this.toastText('获取种植户数据失败');
                    }
                });
            },
            getBatchs: function (deptguid) {
                let that = this;
                this.axios({
                    method: 'get',
                    url: '/farmer/Farmerbatch!viewAll.action',
                    params: {deptguid: deptguid}
                }).then(response => {
                    if (response.data.msg == "success") {
                        that.batchs = response.data.result;
                    } else if (response.data.msg == "fail") {
                        that.batchs = [];
                    } else {
                        this.toastText('获取批次数据失败');
                    }
                });
            },
            getTypes: function () {
                let that = this;
                let postParam = {};
                postParam['entity.dicttype'] = 73;
                this.axios({
                    method: 'get',
                    url: '/farmer/Dict!getDict.action',
                    params: postParam
                }).then(response => {
                    if (response.data.msg == "success") {
                        that.types = response.data.dicts;
                    }else {
                        this.toastText('获取种植户类型数据失败');
                    }
                });
            },
            back: function () {
                this.$parent.switchPage(this.$parent.refView);
            },

        }
    }
</script>
